package com.ruoyi.client.controller;

import com.ruoyi.activity.model.domain.Evaluation;
import com.ruoyi.activity.model.domain.User;
import com.ruoyi.activity.model.domain.Work;
import com.ruoyi.client.model.dto.ApiExpectEvaluationRequest;
import com.ruoyi.activity.service.IEvaluationService;
import com.ruoyi.activity.service.IUserService;
import com.ruoyi.activity.service.IWorkService;
import com.ruoyi.client.vo.UserLoginVO;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.base.BaseException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/api/expect")
public class ApiExpectController {

    @Autowired
    public IWorkService workService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IEvaluationService evaluationService;


    /**
     * 专家查询作品信息列表
     */
    @GetMapping("/list/work")
    public AjaxResult list(Work work, HttpServletRequest request) {
        UserLoginVO userLoginVO = userService.getUserLoginVO(request);
        if (userLoginVO == null) {
            throw new BaseException("你还未登录");
        }
        String identity = userLoginVO.getIdentity();
        if (identity != null && !"评委".equals(identity)) {
            throw new BaseException("你没有权限");
        }
        startPage();
        List<Work> list = workService.selectWorkList(work);
        return AjaxResult.success(list.stream().map(workService::getApiExceptWorkVo).collect(Collectors.toList()));
    }

    /**
     * 评委根据id获取作品详细信息
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/evaluation")
    public AjaxResult getEvaluationById(Long id, HttpServletRequest request) {
        UserLoginVO userLoginVO = userService.getUserLoginVO(request);
        if (userLoginVO == null) {
            throw new BaseException("你还未登录");
        }
        String identity = userLoginVO.getIdentity();
        if (identity != null && !"评委".equals(identity)) {
            throw new BaseException("你没有权限");
        }
        Evaluation evaluation = evaluationService.selectEvaluationById(id);
        if (evaluation == null) {
            throw new BaseException("不存在改评审详情");
        }
        return AjaxResult.success(evaluationService.selectEvaluationById(id));
    }

    /**
     * 修改作品评价信息
     */
    @PutMapping("/psWork")
    public AjaxResult reviewWork(@RequestBody ApiExpectEvaluationRequest apiExpectEvaluationRequest, HttpServletRequest request) {
        UserLoginVO userLoginVO = userService.getUserLoginVO(request);
        if (userLoginVO == null) {
            throw new BaseException("你还未登录");
        }
        String identity = userLoginVO.getIdentity();
        if (identity != null && !"评委".equals(identity)) {
            throw new BaseException("你没有权限");
        }
        boolean reviewResult = evaluationService.reviewWork(apiExpectEvaluationRequest);
        if (!reviewResult) {
            throw new BaseException("更新失败");
        }
        return AjaxResult.success(reviewResult);
    }
}
